//
//  AddSkillsVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/4/17.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "AddSkillsVC.h"
#import "HXTagsView.h"
#import "HXTagAttribute.h"
#import "AnswersListModel.h"
#import "SaveSkillsVC.h"
#import "MoreSkillsVC.h"

@interface AddSkillsVC ()
@property (weak, nonatomic) IBOutlet HXTagsView *tagSelectSkillsView;
@property (strong, nonatomic)  NSMutableArray *skillsSelectArr;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightOfTags;
@property (weak, nonatomic) IBOutlet HXTagsView *moreTagSkillsView;
@property (strong, nonatomic)  NSMutableArray *skillsMoreArr;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@end

@implementation AddSkillsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.countLabel.text = [NSString stringWithFormat:@"%zd",self.skillsArr.count];
    self.navigationItem.title = @"擅长技能";
    self.view.backgroundColor = kWhiteColor;
    self.skillsSelectArr = [NSMutableArray arrayWithArray:self.skillsArr];
    [self loadViewStrar];
    [self reloadSelectTagView];
    [self requestMoreSkills];
    [self loadRightButton];
}
-(void)loadRightButton{
    
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 40, 30);
    [backBtn setTitle:@"下一步" forState:UIControlStateNormal];
    backBtn.titleLabel.font = k14Font;
    [backBtn setTitleColor:UIColorFromRGB(0X474C4D) forState:UIControlStateNormal];
    //    [backBtn setBackgroundImage:[UIImage imageNamed:@"公司icon"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(finishClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,backItem];
    if (IOS11Later) {
        backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    }
}
-(void)finishClick{
    if (self.skillsSelectArr.count >= 5) {
        ALERT_VIEW(@"最多添加5项问答服务");
        return;
    }
    SaveSkillsVC *vc = [[SaveSkillsVC alloc]init];
    vc.skillsArr = self.skillsSelectArr;
    [self.navigationController pushViewController:vc animated:YES];
}
/*
 方法描述:
 初始化页面ui
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)loadViewStrar{
    // Do any additional setup after loading the view from its nib.
    _tagSelectSkillsView.completion = ^(NSArray *selectTags,NSInteger currentIndex) {
        NSLog(@"selectTags:%@ currentIndex:%ld",selectTags, (long)currentIndex);
        [self.skillsSelectArr removeObjectAtIndex:currentIndex];
        [self reloadSelectTagView];
        [self reloadMoreTagView];
    };
    _tagSelectSkillsView.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    HXTagAttribute *model = [[HXTagAttribute alloc]init];
    model.borderWidth  = 0.5;
    model.borderColor  = UIColorFromRGB(0x7ECCD8);
    model.cornerRadius  = 12;
    model.titleSize  = 11;
    model.textColor  = UIColorFromRGB(0x7ECCD8);
    model.selectedTextColor  = UIColorFromRGB(0x7ECCD8);
    
    model.normalBackgroundColor  = UIColorFromRGB(0xffffff);
    model.selectedBackgroundColor  = UIColorFromRGB(0xffffff);
    model.tagSpace  = 15;
    _tagSelectSkillsView.tagAttribute = model;
    HXTagCollectionViewFlowLayout *flowLayout = [[HXTagCollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsZero;
    flowLayout.itemSize = CGSizeMake(64,24);
    flowLayout.sectionInset = UIEdgeInsetsMake(0.0f, 15.0f, 10.0f, 10.0f);
    _tagSelectSkillsView.layout = flowLayout;
    
    [_tagSelectSkillsView reloadData];
    
    // Do any additional setup after loading the view from its nib.
    _moreTagSkillsView.completion = ^(NSArray *selectTags,NSInteger currentIndex) {
        NSLog(@"selectTags:%@ currentIndex:%ld",selectTags, (long)currentIndex);
        if (self.skillsSelectArr.count > 5) {
            ALERT_VIEW(@"最多添加5项问答服务");
            [self reloadMoreTagView];
        }else{
            AnswersListModel *model = self.skillsMoreArr[currentIndex];
            [self.skillsSelectArr addObject:model];
            [self reloadSelectTagView];
        }
        
    };
    _moreTagSkillsView.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    HXTagAttribute *model1 = [[HXTagAttribute alloc]init];
    model1.borderWidth  = 0.5;
    model1.borderColor  = UIColorFromRGB(0x87CFDB);
    model1.cornerRadius  = 14;
    model1.titleSize  = 13;
    model1.textColor  = UIColorFromRGB(0x87CFDB);
    model1.selectedTextColor  = UIColorFromRGB(0xffffff);
    
    model1.normalBackgroundColor  = UIColorFromRGB(0xE2F2F4);
    model1.selectedBackgroundColor  = UIColorFromRGB(0x7ECCD8);
    model1.tagSpace  = 15;
    _moreTagSkillsView.tagAttribute = model1;
    HXTagCollectionViewFlowLayout *flowLayout1 = [[HXTagCollectionViewFlowLayout alloc] init];
    flowLayout1.minimumInteritemSpacing = 10;
    flowLayout1.minimumLineSpacing = 10;
    flowLayout1.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout1.sectionInset = UIEdgeInsetsZero;
    flowLayout1.itemSize = CGSizeMake(40,28);
    flowLayout1.sectionInset = UIEdgeInsetsMake(0.0f, 15.0f, 10.0f, 10.0f);
    _moreTagSkillsView.layout = flowLayout1;
    
    [_moreTagSkillsView reloadData];
}
/*
 方法描述:
 刷新选中的标签
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)reloadSelectTagView{
    NSMutableArray *selectedArr = [NSMutableArray array];
    for (NSInteger i = 0; i < self.skillsSelectArr.count; i++) {
        AnswersListModel *model = self.skillsSelectArr[i];
        [selectedArr addObject:model.skillName];
    }
    _tagSelectSkillsView.tags = selectedArr;
    
    [_tagSelectSkillsView reloadData];
    CGFloat height = [HXTagsView getHeightWithTags:self.tagSelectSkillsView.tags layout:self.tagSelectSkillsView.layout tagAttribute:self.tagSelectSkillsView.tagAttribute width:kScreenWidth];
    self.heightOfTags.constant = height;
    self.countLabel.text = [NSString stringWithFormat:@"%zd",self.skillsSelectArr.count];
}
/*
 方法描述:
 请求热门标签
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)requestMoreSkills{
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:@{} url:@"basics/getHotSkills" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            self.skillsMoreArr = [AnswersListModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
            [self reloadMoreTagView];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}
/*
 方法描述:
 刷新更多标签
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)reloadMoreTagView{
    NSMutableArray *moreArr = [NSMutableArray array];
    for (NSInteger i = 0; i < self.skillsMoreArr.count; i++) {
        AnswersListModel *model = self.skillsMoreArr[i];
        [moreArr addObject:model.skillName];
    }
    _moreTagSkillsView.isMultiSelect = YES;
    _moreTagSkillsView.tags = moreArr;
    
    NSMutableArray *selectedArr = [NSMutableArray array];
    for (NSInteger i = 0; i < self.skillsSelectArr.count; i++) {
        AnswersListModel *model = self.skillsSelectArr[i];
        [selectedArr addObject:model.skillName];
    }
    _moreTagSkillsView.selectedTags = selectedArr;
    [_moreTagSkillsView reloadData];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)moreSkillsClick:(id)sender {
    if (self.skillsSelectArr.count >= 5) {
        ALERT_VIEW(@"最多添加5项问答服务");
        return;
    }
    MoreSkillsVC *vc = [[MoreSkillsVC alloc]init];
    vc.selectBlock = ^(SkillModel *skillModel){
        for (NSInteger i = 0; i<self.skillsSelectArr.count; i++) {
            AnswersListModel *model = self.skillsSelectArr[i];
            if (skillModel.skillId == model.skillId) {
                ALERT_VIEW(@"该技能已经添加了。");
                return ;
            }
        }
        AnswersListModel *model = [[AnswersListModel alloc]init];
        model.price = 0;
         model.skillName = skillModel.skillName;
         model.incumbentQuestionId = 0;
         model.skillId = skillModel.skillId;
     
        
        [self.skillsSelectArr addObject:model];
        [self reloadSelectTagView];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
